%to download 1 min data in iaga 2002 format from ftp edin,
% upzip, concatenate , read, convert (if neces tofrom HDZ to XYZ format)

function[X_fday,Y_fday,Z_fday,X_ts,Y_ts,Z_ts] = obs_1_min_read(fname)


%fid = fopen('C:\Manoj\temp\cbb1979m\test.txt','rt');
fid = fopen(fname,'rt');
if fid == -1,
    display('Error in opening a file');
    return;
end;
    
A= fscanf(fid,'%c',[402,inf]);
if any(A(:,end) == ''),
    A(:,end) = [];
end;
fclose(fid);
y=str2num(A(13:14,:)');

y = y+2000; % oops Y2K bug. Doesn't work for 20th century


m=str2num(A(15:16,:)');
d=str2num(A(17:18,:)');
h=str2num(A(20:21,:)');
fday = datenum(y,m,d,h,0,0);
cmp = upper(A(19,:)');
%data = sscanf(A(35:394,:),'%6d',[60,size(A,2)]); %doesn't work if the
%characters are contin
data = zeros([60,length(fday)]);
st = 35;
en = 40;
for i = 1:60,
    data(i,:) = str2num(A(st:en,:)');
    st = en + 1;
    en = st + 5;
end;

%L = data == 999999 | abs(data) > 80000;
L = data == 999999 ;

data(L) = NaN;

%Some files are special :-)

% if strcmp(fname,'D:\Manoj\imag19922004\WDCfromftp\unzippedfiles\aqu97all.wdc') == 1,
%      data(data == 99999) = NaN;
% end;


AllComp = unique(cmp);
if any(ismember(upper(AllComp),['H';'D'])) 
LD = cmp == 'D';
LH = cmp == 'H';

if any(fday(LD)-fday(LH)), %time stamp mismatch between the  components
    display('error LD and LH are not identical');
    return;
else
X = data(:,LH).*cos(data(:,LD)/10/60*pi/180); % D 0.1 min to rads 
Y = data(:,LH).*sin(data(:,LD)/10/60*pi/180); % D 0.1 min to rads 
data(:,LH) = X;
data(:,LD) = Y;
cmp(LH) = 'X';
cmp(LD) = 'Y';
clear X Y;  
end;
end;

%all data are in XYZ and F
%reshape the data into time series

minutely = datenum(0,0,0,0,0:59,30);

L = cmp == 'X';

X_ts = reshape(data(:,L),[1,sum(L)*60]);
X_fday = reshape(repmat(minutely',[1,sum(L)]) + repmat(fday(L)',[60,1]),[1,60*sum(L)]);

L = cmp == 'Y';

Y_ts = reshape(data(:,L),[1,sum(L)*60]);
Y_fday = reshape(repmat(minutely',[1,sum(L)]) + repmat(fday(L)',[60,1]),[1,60*sum(L)]);

L = cmp == 'Z';

Z_ts = reshape(data(:,L),[1,sum(L)*60]);
Z_fday = reshape(repmat(minutely',[1,sum(L)]) + repmat(fday(L)',[60,1]),[1,60*sum(L)]);


% if isequal(sum(cmp=='X'),sum(cmp=='Z'),sum(cmp=='Y')) & sum(cmp=='X') == size(A,2)/3,
% %data are X Y Z
% display('OK');
% elseif isequal(sum(cmp=='H'),sum(cmp=='D'),sum(cmp=='Z')) & sum(cmp=='H') == size(A,2)/3,
% %data are in HDZ format
%  %minutely(:,2) = minutely(:,2)/10/60*pi/180; % D 0.1 min to rads
%  end;




% COLUMNS   FORMAT   DESCRIPTION
% 
% 1-6       I6       Observatory's North Polar distance.  
%                    0� to 180� from the north geographic pole in thousandths 
%                    of a degree. Decimal point is implied between positions 3 
%                    and 4.
% 7-12      I6       Observatory's Geographic longitude.  
%                    0� to 360� measured east from Greenwich in thousandths 
%                    of a degree. Decimal point is implied between positions 9 
%                    and 10.
% 13-14     I2       Year. Last 2 digits, 1996 = 96. See also column 26.
% 15-16     I2       Month (01-12)
% 17-18     I2       Day of month (01-31)
% 19        Al       Element (D,I,H,X,Y,Z, or F)
% 20-21     I2       Hour of day (00-23)
% 22-24     A3       Observatory 3-letter code.
% 25        A1       Arbitrary.
% 26        I1       Century digit.
%                    Year = 2014, Century digit = 0.
%                    Year = 1889, Century digit = 8.
%                    Year = 1996, Century digit = 9 or 'SPACE' for backwards
%                    compatibility.
% 27        A1       Preliminary or Definitive data.
%                    Preliminary = P , Definitive = D
% 28-34     A7       Blanks
% 35-394    60I6     60 6-digit 1-minute values for the given element for that
%                    data hour.
%                    The values are in tenth-minutes for D and I, and in
%                    nanoTeslas for the intensity elements.
% 395-400   I6       Hourly Mean.
%                    The average of the preceeding 60 1-minute values.
% 401-402            Record end marker.
%                    Two chars 'cr'= 13 and 'nl'= 10.
